<?php  if ( ! defined('IN_DiliCMS')) exit('No direct script access allowed');
	class Ticket_m extends CI_Model{
		function __construct()
		{
			parent::__construct();
		}
			
		function getCommendTicket($model=0,$limitNum)	//获取推荐门票
		{
			$datawhere=" and model=$model ";
			$sql="select  id,ticketTitle,pricePer,otherPrice,transportation,destination,departureDate,introduction,name,folder,type  from dili_u_m_ticket as t inner join dili_attachments as a on a.content=t.id where recommend=1 $datawhere group by content limit $limitNum";
		//	print_r($sql);
			$CommendTicket=$this->db->query($sql)->result();
			return $CommendTicket;
		}
		function getHotTicket($model=0,$limitNum)	//获取热门门票
		{
			$datawhere=" and model=$model ";
			$sql="select  id,ticketTitle,pricePer,otherPrice,transportation,destination,departureDate,introduction,name,folder,type  from dili_u_m_ticket as t inner join dili_attachments as a on a.content=t.id where hot=1 $datawhere group by content limit $limitNum";
			$HotTicket=$this->db->query($sql)->result();
			return $HotTicket;
		}
		function getLastTicket($model=0,$limitNum)	//获取最新门票
		{
			$datawhere=" and model=$model ";
			$sql="select  id,ticketTitle,pricePer,otherPrice,transportation,destination,departureDate,introduction,name,folder,type  from dili_u_m_ticket as t inner join dili_attachments as a on a.content=t.id where 1=1 $datawhere group by content order by t.id desc limit $limitNum";
			$LastTicket=$this->db->query($sql)->result();
			return $LastTicket;
		}
		function getIndexLastTicket($model=0)	//获取首页最新门票
		{
			$datawhere=" and model=$model ";
			$sql="select  id,ticketTitle,pricePer,otherPrice,transportation,destination,departureDate  from dili_u_m_ticket  order by id desc limit 9";
			$LastTicket=$this->db->query($sql)->result();
			return $LastTicket;
		}
		function getCityList($cityId=0,$model=0,$begin_record=0,$per_page=10){
			if((int)$cityId){
					$datawhere="";
					$datawhere=" and  INSTR(destination,',$cityId,') and model=$model ";
				if(!$begin_record){$begin_record=0;}
				$Tfields="id,ticketTitle,pricePer,otherPrice,transportation,destination,departureDate,name,folder,type";
				$sql="select ".$Tfields." from  dili_u_m_ticket as t left join dili_attachments as a on t.id=a.content where 1=1 $datawhere group by id limit $begin_record,$per_page";
				//print_r($sql);					
				$CityList=$this->db->query($sql)->result();
				return $CityList;
			}
		}
		function getTicketnum($cityId=0){
			if((int)$cityId){
				$datawhere="";
					$datawhere=" and  INSTR(destination,',$cityId,') ";
				$sql="select count(id) as icount  from  dili_u_m_ticket  where 1=1 $datawhere ";
				$Ticketnum=$this->db->query($sql)->row_array();
				return $Ticketnum["icount"];
			}
		}
		function getTicketInfo($tid=0)	//获取文章内容
		{
			if ($tid){
				$Tfields="id,ticketTitle,pricePer,otherPrice,transportation,destination,departureDate,create_time,introduction,item";
				$sql="select $Tfields from  dili_u_m_ticket where id=".$tid."  limit 1 ";
				$TicketInfo["info"]=$this->db->query($sql)->result();
				$sql_annex="select content,model,name,folder,type from  dili_attachments  where content=$tid  and model=13 limit 1";
				$TicketInfo["annex"]=$this->db->query($sql_annex)->result();				
				return $TicketInfo;
			}else{
				return false;
			}
		}			
		function saveTicketOrder($tid=0,$pricePer=0,$orderNum=0,$departureDate,$RealName,$phone,$Tel,$qqNum,$Email,$Remark)
		{
			// 0=资讯|1=新闻|2=文化|3=攻略|4=常识|5=问答
			$time = time();
			//$date = date('Y-m-d H:i:s',time()+8*3600); 
			$strSql = "INSERT INTO dili_u_m_ticketorder(create_time,
										  update_time,
										  tid,
										  pricePer,
										  orderNum,
										  departureDate,
										  RealName,
										  phone,
										  Tel,
										  qqNum,
										  Email,
										  Remark
										  ) 
								  VALUES( $time,$time,$tid,$pricePer,$orderNum,'$departureDate','$RealName','$phone','$Tel','$qqNum','$Email','$Remark')";
			$status["success"]=$this->db->query($strSql);
			$status["oid"]=$this->db->insert_id();
			return $status;
		}		
		function getOrderInfo($oid=0){
			$sql="select * from dili_u_m_ticketorder where id=$oid";
			return $this->db->query($sql)->result();
		}
		function getLastTicketOrder($model=0,$limitNum){
			$datawhere=" and model=$model ";
			$sql="select o.*,t.ticketTitle from dili_u_m_ticketorder as o,dili_u_m_ticket as t where o.tid=t.id order by o.id desc  limit $limitNum";
			$LastTicketOrder["list"]=$this->db->query($sql)->result();
			foreach($LastTicketOrder["list"] as $info){
				$sql_annex="select content,model,name,folder,type from  dili_attachments  where content=".$info->tid."  $datawhere  group by content limit 1";			
				//echo 	$sql_annex;	
				$LastTicketOrder['arr'][$info->tid]=$this->db->query($sql_annex)->result();	;		//门票id做索引
			}		
			//print_r($LastTicketOrder['arr']);	
			return $LastTicketOrder;
		}
		
		
	}